package com.baimo.support.utils;

import com.baimo.support.bean.map.LocationResult;
import com.baimo.support.bean.map.MapAddressBean;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class MapUtils {
	private static String outPut = "json";
	private static String ak = "f5VNdyy08EkuvOPtXWMb5d9B";
	private static String mapApiUrl = "http://api.map.baidu.com/geocoder/v2/?";
	private static String perimeterDisable = "0";
	private static String perimeterEnable = "1";
	private static Gson gson = new Gson();
	public static LocationResult sendGetLocation(String location) throws ClientProtocolException, IOException{
		String url = mapApiUrl + "address=" + location + "&output=" + outPut + "&ak=" + ak;
		String result = "";
		HttpGet request = new HttpGet(url);
		HttpResponse response = HttpClients.createDefault().execute(request);
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity());
		}else{
			return new LocationResult();
		}
		System.out.println(result);
		return gson.fromJson(result, LocationResult.class);
	}
	public static MapAddressBean sendGetAddress(String lng, String lat) throws ClientProtocolException, IOException{
		String url = mapApiUrl + "ak=" + ak +"&location=" + lat + "," +lng +"&output=" + outPut +"&pois=" + perimeterDisable;
//		System.out.println(url);
		String result = "";
		HttpGet request = new HttpGet(url);
		HttpResponse response = HttpClients.createDefault().execute(request);
		if(response.getStatusLine().getStatusCode()==200){
			result = EntityUtils.toString(response.getEntity(),"utf-8");
		}
		return gson.fromJson(result,MapAddressBean.class);
	}
	    private final static double PI = 3.14159265358979323; //
	    private final static double R = 6371229; //

	    public static double getDistance(double longt1, double lat1, double longt2,double lat2) {
	        double x, y, distance;
	        x = (longt2 - longt1) * PI * R
	                * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
	        y = (lat2 - lat1) * PI * R / 180;
	        distance = Math.hypot(x, y);
	        return distance;
	    }
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
//		System.out.println(MapUtils.sendGetLocation("上海市溧阳路649号东楼").getResult().getLocation());
		System.out.println(MapUtils.sendGetAddress("113.53373098039063","22.265599835349549"));
//		System.out.println(MapUtils.sendGetAddress("121.4972440000673","31.26002487046417").getResult().getAddressComponent().getProvince());
//		System.out.println(MapUtils.sendGetAddress("121.4972440000673","31.26002487046417").getResult().getAddressComponent().getCity());
//		System.out.println(getDistance(121.49696762829,31.260172083662,116.33595335539,39.999689512438));
//		URLDecoder.decode("\u65e0\u76f8\u5173\u7ed3\u679c");
//		System.out.println("decode--->" + URLDecoder.decode("\u65e0\u76f8\u5173\u7ed3\u679c", "utf-8"));
//		Gson gson = new Gson();
//		LocationResult location = gson.fromJson(MapUtils.sendGetLocation("�Ϻ��к��������·649��"),LocationResult.class);
//		System.out.println(location.getResult().getLocation().getLng());
//		System.out.println(location.getResult().getLocation().getLat());
		
//		System.out.println(sendGetAddress("116.33595335539","39.999689443348"));
	}
}
